<template>
	<view class="container">
		<view class="tops">
			<view class="titles"> 海外招聘 </view>
			<scroll-view class="scroll-tab" scroll-x :show-scrollbar="false">
				<view class="flex-view">
					<view class="top-news" v-for="item in jobList">
						<view class="news-img">
							<image lazy-load :src="item.thumb" mode=""></image>
						</view>
						<view class="news-title">{{item.title}}</view>
						<view class="news-publish_time">发布时间：{{item.add_time}}</view>
					</view>
				</view>
			</scroll-view>
		</view>
		<view class="notices">
			 <view class="notice-bar">
			    <text class="title">签约动态：</text>
			    <view class="divider"></view>
			    <view class="notice-content">
					<view class="notice-list" :style="{ transform: `translateY(-${currentIndex * 60}rpx)` }" >
						<view v-for="(item, index) in notices" :key="index" class="notice-item" > {{ item.add_time }}  {{ item.title }} </view>
					</view>
			    </view>
			    <image src="/static/arrow-right.png" class="arrow" @click="handleClick" />
			  </view>
		</view>
		<view class="news-list">
			<view class="titles"> 行业资讯 </view>
			<scroll-view scroll-y :style="{height: scrollViewHeight + 'px'}">
				<!-- 内容 -->
				<view class="news" v-for="item in infoList" @click="toDetail(item.id)">
					<view class="news-left">
						<view class="news-title"> {{item.title}} </view>
						<view class="times">
							<view class="add_time"> {{item.add_time}} </view>
							<view class="reads">
								<view class="reads-img">
									<image src="/static/reads.png" mode=""></image>
								</view>
								<view class="news-reads"> {{item.click}} </view>
							</view>
						</view>
					</view>
					<view class="news-imgs"> 
						<image lazy-load :src="item.thumb"></image>
					</view>
				</view>			
			</scroll-view>			
		</view>		
	</view>
	
	<!--自定义TabBar-->
	<uv-tabbar :value="value" :fixed="true" @change="index=>value = index" activeColor="#F56822">
		<view class="tabbars">
			<uv-tabbar-item text="在线好课" @click="tabbarClick(1)">
				<template v-slot:active-icon>
					<image class="icon" src="/static/tabbar/index-check.png"></image>
				</template>
				<template v-slot:inactive-icon>
					<image class="icon" src="/static/tabbar/index.png"></image>
				</template>
			</uv-tabbar-item>
			<uv-tabbar-item text="我的课程" @click="tabbarClick(2)">
				<template v-slot:active-icon>
					<image class="icon" src="/static/tabbar/class-check.png"></image>
				</template>
				<template v-slot:inactive-icon>
					<image class="icon" src="/static/tabbar/class.png"></image>
				</template>
			</uv-tabbar-item>
			<uv-tabbar-item text="汉语动态" @click="tabbarClick(3)">
				<template v-slot:active-icon>
					<image class="icon" src="/static/tabbar/chinese-check.png"></image>
				</template>
				<template v-slot:inactive-icon>
					<image class="icon" src="/static/tabbar/chinese.png"></image>
				</template>
			</uv-tabbar-item>
			<uv-tabbar-item text="个人中心" @click="tabbarClick(4)">
				<template v-slot:active-icon>
					<image class="icon" src="/static/tabbar/user-check.png"></image>
				</template>
				<template v-slot:inactive-icon>
					<image class="icon" src="/static/tabbar/user.png"></image>
				</template>
			</uv-tabbar-item>
		</view>
	</uv-tabbar>
</template>

<script>
	export default {
		data() {
			return {
				jobList: [ ],
				infoList: [ ],
				notices: [],
				currentIndex: 0,
				timer: null,
				baseURL: getApp().globalData.baseUrl,
				scrollViewHeight: 0,
				currentPage: 1, 
				pageSize: 10,
				total: 0,  
				isLoading: false, 
				isRefreshing: false,
				noMore: false,
				value: 2
			}
		},
		onReady() {
			const query = uni.createSelectorQuery().in(this);
			query.select('.news-list').boundingClientRect(data => {
				this.scrollViewHeight = data.height - 60; 
			}).exec();
		},
		beforeDestroy() {
		    this.stopAutoScroll()
		},
		onShow(){
			this.getList()
			this.startAutoScroll()
		},
		methods: {
			startAutoScroll() {
				this.timer = setInterval(() => {
					this.currentIndex = (this.currentIndex + 1) % this.notices.length
				}, 3000)
			},
			stopAutoScroll() {
			  clearInterval(this.timer)
			},
			handleClick() {
			  uni.showToast({
				title: '查看更多公告',
				icon: 'none'
			  })
			},
			getList(){
				var appid = uni.getStorageSync('appid')
				uni.request({
					url: this.baseURL + '/api/index/news',
					method: 'GET',
					data: {appid: appid},
					header: { 'Content-Type': 'application/json' },
					success: (res) => {
						  this.jobList  = res.data.data.job
						  this.infoList = res.data.data.info
						  this.notices  = res.data.data.sign
						  if (this.currentPage === 1) {
						  	this.infoList = res.data.data.info
						  } else {
						  	this.infoList = [...this.newsList, ...res.data.data.info]
						  }
						  this.isLoading = false
						  this.isRefreshing = false
						  if( this.infoList.length >= res.data.data.count ){
						  	this.noMore = false
						  } else {
						  	this.noMore = true
						  }
					},
					fail: (err) => {
						console.error('请求失败:', err);
					}
				})
			},
			// 下拉刷新
			onRefresh() {
				if (this.isRefreshing) return
				this.isRefreshing = true
				this.currentPage = 1
				this.getList()
			},
			// 上拉加载更多
			loadMore() {
				if (this.isLoading || this.noMore) return
				this.currentPage += 1
				this.getList()
			},
			// 跳转详情页
			toDetail(id) {
				uni.navigateTo({
					url : '/pages/dynamics/details?id='+id
				})
			},
			tabbarClick(item){
				if( item == 1 ){
					uni.reLaunch({url: '/pages/index/index'})
				} else if( item == 2 ){
					uni.reLaunch({url: '/pages/class/class'})
				} else if( item == 3 ){
					uni.reLaunch({url: '/pages/dynamics/dynamics'})
				} else if( item == 4 ){
					uni.reLaunch({url: '/pages/user/user'})
				}
			}
		}
	}
</script>

<style>
	.container{background: #F8FAFB;background: #F8FAFB;display: flex;flex-direction: column;height: 100vh;width: 100%;}
	.titles{font-size: 42rpx;font-weight: bold;width: 690rpx;margin: 0 auto;padding: 10rpx 0 10rpx 0;}
	.scroll-tab{height: 340rpx;width: 100%;}
	.flex-view{width: 100%;display: flex;flex-direction: row;}
	.top-news{width: 230rpx;height: 320rpx;border-radius: 10rpx;margin-left: 20rpx;background: #fff;flex-shrink: 0; }
	.news-img{width: 100%;height: 130rpx;}
	.news-img image{width: 100%;height: 100%;}
	.news-title{width: 98%;margin: 10rpx auto;display: -webkit-box;-webkit-box-orient: vertical;-webkit-line-clamp: 2;overflow: hidden;text-overflow: ellipsis;word-break: break-all;font-size: 28rpx;}
	.news-publish_time{width: 98%;margin: 10rpx auto;display: -webkit-box;-webkit-box-orient: vertical;-webkit-line-clamp: 2;overflow: hidden;text-overflow: ellipsis;word-break: break-all;color: #888;font-size: 26rpx;}
	.notices{padding: 10rpx 15rpx;margin: 20rpx;}
	.notice-bar {display: flex;align-items: center;background-color: #fff;border-radius: 10rpx;flex-direction: row;}
	.title {font-size: 28rpx;color: #333;flex-shrink: 0;padding-left: 10rpx;}
	.divider {width: 1px;height: 20px;background-color: #ddd;margin: 0 12px;flex-shrink: 0;}
	.notice-content {flex: 1;height: 60rpx;overflow: hidden;position: relative;}
	.notice-list {position: absolute;top: 0;left: 0;right: 0;transition: transform 0.6s ease-in-out;}
	.notice-item {height: 60rpx;line-height: 60rpx;font-size: 26rpx;color: #F56822;white-space: nowrap;overflow: hidden;text-overflow: ellipsis;}
	.arrow {width: 50rpx;height: 50rpx;flex-shrink: 0;margin-right: 20rpx;}
	.news-list{flex: 1;overflow: hidden;}
	.news-list scroll-view {height: 100%;}
	.news{width: 690rpx;border-radius: 10rpx;margin: 10rpx auto;background: #fff;display: flex;flex-direction: row;padding: 20rpx;}
	.news .news-left{width: 500rpx;}
	.news .news-title{font-size: 28rpx;}
	.news .add_time{font-size: 26rpx;color: #888;}
	.news .news-imgs{width: 150rpx;border-radius: 10rpx;}
	.news .news-imgs image{width: 100%;height: 100%;}
	.news .times{display: flex;flex-direction: row;justify-content: space-between;padding: 40rpx 20rpx 0 10rpx;color: #888;}
	.news .reads-img{width: 30rpx;height: 30rpx;}
	.news .reads-img image{width: 30rpx;height: 30rpx;}
	.news .reads{display: flex;flex-direction: row;justify-content: center;padding-right: 20rpx;}
	.news .news-reads{padding-left: 10rpx;font-size: 26rpx;}
	.icon {width: 50rpx;height: 50rpx;}
	.tabbars{ margin-top: 20rpx;display: flex;flex-direction: row;justify-content: space-around;width: 100%;padding-bottom: 20rpx; }
</style>
